package com.redis.config;

import com.redis.constant.OrderMQConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 定义延迟交换机
 */
@Configuration
public class RabbitMQDelayedConfig {
    /**
     * 自动取消订单
     * @return
     */
    @Bean(OrderMQConstants.CANCEL_ORDER_QUEUE)
    public Queue delayCancelQueue(){return new Queue(OrderMQConstants.CANCEL_ORDER_QUEUE);}

    /**
     *
     * @return
     */
    @Bean(OrderMQConstants.CANCEL_ORDER_EXCHANGE)
    public CustomExchange delayedCancelExchange(){
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-delayed-type","direct");
        /**
         * 1、交换机名称
         * 2、交换机类型
         * 3、是否需要持久化
         * 4、是否需要自动删除
         * 5、其他参数
         */
        return new CustomExchange(OrderMQConstants.CANCEL_ORDER_EXCHANGE,"x-delayed-message",true,false,arguments);
    }

    /**
     * 绑定队列和延迟交换机
     * @return
     */
    @Bean
    public Binding delayCancelBinding(){
        return BindingBuilder.bind(delayCancelQueue()).to(delayedCancelExchange()).with(OrderMQConstants.CANCEL_ORDER_ROUTING_KEY).noargs();
    }

    /**
     * 自动收货
     * @return
     */
    @Bean(OrderMQConstants.RECEIVED_ORDER_QUEUE)
    public Queue delayReceivedQueue(){return new Queue(OrderMQConstants.RECEIVED_ORDER_QUEUE);}

    /**
     *
     * @return
     */
    @Bean(OrderMQConstants.RECEIVED_ORDER_EXCHANGE)
    public CustomExchange delayedReceivedExchange(){
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-delayed-type","direct");
        /**
         * 1、交换机名称
         * 2、交换机类型
         * 3、是否需要持久化
         * 4、是否需要自动删除
         * 5、其他参数
         */
        return new CustomExchange(OrderMQConstants.RECEIVED_ORDER_EXCHANGE,"x-delayed-message",true,false,arguments);
    }

    /**
     * 绑定队列和延迟交换机
     * @return
     */
    @Bean
    public Binding delayReceivedBinding(){
        return BindingBuilder.bind(delayReceivedQueue()).to(delayedReceivedExchange()).with(OrderMQConstants.RECEIVED_ORDER_ROUTING_KEY).noargs();
    }
}